﻿using System;
using com.yoozoo.gta.Gameplay.RTS;
using TMPro;
using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.RTS
{
    [ExecuteInEditMode]
    public class BaseArmyInfo : MonoBehaviour
    {
        #region 详细信息

        /// <summary>
        /// 士兵数
        /// </summary>
        //[SerializeField] private TMP_Text Panel1SoldierCountText;
        //[SerializeField] public YImage soldierIcon;

        [SerializeField] private TMP_Text SpreadPanel1SoldierCountText;
        [SerializeField] public YImage SpreadSoldierIcon;

        public TMP_Text playerName;

        public Transform SelectedInfo;
        public Collider TsanCollider;
        private GameObject SoldierInfo;
        private Vector3 iconPos;

        private bool isShow;

        private bool solCountVisiable = true;
        
        //private Transform bgTransform;

        public Vector3 IconPos
        {
            get => this.iconPos;
        }

        #endregion

        public void SetName(string name)
        {
            if (name!=playerName.text)
            {
                playerName.text = name;
            }
        }


        public void SetCount(int count)
        {
//            if (isShow)
//            {
                if (solCountVisiable)
                {
                    var str = BattleUtils.GetNumStr(count);
                    SpreadPanel1SoldierCountText.text = str;
                }else
                {
                    SpreadPanel1SoldierCountText.text = "??????";
                }
//            }
        }

        public void SetTextColor(Color c)
        {
            if (c!= playerName.color)
            {
                playerName.color = c;
            }
        }

        public void SetVisible(bool visible)
        {
            SelectedInfo.transform.localScale = visible ? Vector3.one : Vector3.zero;
        }

        public void SetSoldierCountVisible(bool visible)
        {
            SpreadPanel1SoldierCountText.transform.localScale = visible ? Vector3.one : Vector3.zero;
        }

        public void SetSoldierIconVisible(bool visible)
        {
            SpreadSoldierIcon.transform.parent.localScale = visible ? Vector3.one : Vector3.zero;
        }
        public void SetSelect(bool select, bool isInBattleState, bool _forceShowHud,bool _isBattleStyle)
        {
            //SoldierInfo.transform.localScale = select ? Vector3.zero : Vector3.one;
            if (_forceShowHud)
            {
                if (select || !_isBattleStyle)
                {
                    SelectedInfo.transform.localScale = Vector3.one;
                    isShow = true;
                    return;
                }
            }
            if (isInBattleState)
            {
                SelectedInfo.transform.localScale = select ? Vector3.one : Vector3.zero;
                isShow = select;
            }
            else
            {
                if (select)
                {
                    if (SelectedInfo.transform.localScale == Vector3.zero)
                    {
                        SelectedInfo.transform.localScale = Vector3.one;
                        isShow = true;
                    }
                }
            }
        }
       
        private void Awake()
        {
            this.isShow = true;
            SoldierInfo = transform.Find("bg/soldierInfoSpread").gameObject;
        }

        /// <summary>
        /// 修改士兵信息的显示状态
        /// </summary>
        public void ChangeSoldierInfoActive(bool active)
        {
            SoldierInfo.SetActive(active);
        }

        /// <summary>
        /// 修改士兵数量的显示状态(所有除自己外的士兵数量显示为???)
        /// </summary>
        /// <param name="visiable"></param>
        private void ChangeSoldierCountVisiable(bool visiable)
        {
            solCountVisiable = visiable;
        }

        /// <summary>
        /// 根据部队阵营修改士兵数量的显示状态(所有除自己外的士兵数量显示为???)
        /// </summary>
        /// <param name="team"></param>
        public void ChangeSoldierCountVisiableByTeam(int team)
        {
            if(team == (int)MarchTeam.Ally || team == (int)MarchTeam.Self)
            {
                ChangeSoldierCountVisiable(true);
            }
            else
            {
                ChangeSoldierCountVisiable(false);
            }
        }

        public void ChangeSoldierCountVisibleForce(bool visible)
        {
            ChangeSoldierCountVisiable(visible);
        }

        public void SetSoldierTypeIcon(RtsServerInfo serverData)
        {
            if(SpreadSoldierIcon != null)
            {
                /*if (lod1Data.initSoldierCount == 0)
                    return;*/
                //soldierIcon.sprite = ...;//

                string type;
                
                
                //集结部队,且为leader统一用混兵
                //车头如果在阵亡时更新是没有士兵数量的(包括类型)
                if (NeedShowMixedSoldierIcon(serverData))
                {
                    type = "icon_setoff_mixedstable";
                }
                else
                {
                    var soldierType = serverData.SoldierType;

                    switch (soldierType)
                    {
                        case 1:
                            type = "icon_setoff_barracks";
                            break;
                        case 2:
                            type = "icon_setoff_stable";
                            break;
                        case 3:
                            type = "icon_setoff_range";
                            break;
                        case 4:
                            type = "icon_setoff_siege";
                            break;
                        default:
                            type = "icon_setoff_barracks";
                            break;
                    }
                }
                
                ArmyManager.Instance.ResourceManager.SetHeroTypeIcon(type, SpreadSoldierIcon);
            }
        }
        
        private bool NeedShowMixedSoldierIcon(RtsServerInfo serverData)
        {
            //有集结部队,且在集结状态需要显示混兵
            if ((serverData.marchType == MarchType.ASSEMBLY_JOIN_11 || serverData.marchType == MarchType.ASSEMBLY_OUT_12) &&
                serverData.assemblyId > 0 && serverData.isAssemblyLeader && serverData.initSoldierCount > 0)
            {
                
                return true;
            }
            
            return false;
        }
        
        public void SetColliderInfo(string info)
        {
            if (TsanCollider)
            {
                TsanCollider.gameObject.name = info;
            }
        }
        
        public void SetColliderActive(bool isMarch, RtsServerInfo serverData, bool isShow)
        {
            bool active = isMarch;
            if (TsanCollider)
            {
                if (serverData.team == (int)MarchTeam.Self)
                {
                    TsanCollider.gameObject.SetActive(false);
                    return;
                }

                if (active)
                {
                    if (isShow)
                    {
                        if (serverData.marchType == MarchType.ATTACK_1 || serverData.marchType == MarchType.GARRISON_2 ||
                            serverData.marchType == MarchType.RETURN_4 )
                        {
                            TsanCollider.gameObject.SetActive(true);
                        }
                        return;
                    }
                }
                TsanCollider.gameObject.SetActive(false);
            }
        }
        
    }
}
